﻿namespace Dcms.OleDbDAL
{
    using Dcms.BaseLib;
    using Dcms.IDAL;
    using Dcms.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.OleDb;

    public class Dcms_RoleDAL : IDcms_RoleDAL
    {
        public Dcms_RoleModel AddModel(Dcms_RoleModel model)
        {
            string sQLString = "insert into Dcms_Role(Role_Name,Role_Order,Role_level) values(@Role_Name,@Role_Order,@Role_level)";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@Role_Name", OleDbType.VarChar), new OleDbParameter("@Role_Order", OleDbType.Integer), new OleDbParameter("@Role_level", OleDbType.Integer) };
            cmdParms[0].Value = model.Role_Name;
            cmdParms[1].Value = model.Role_Order;
            cmdParms[2].Value = model.Role_level;
            model.Role_Id = OleDbHelper.ExecuteSql(sQLString, cmdParms);
            return model;
        }

        public IList<Dcms_RoleModel> DataSetToList(DataSet ds)
        {
            IList<Dcms_RoleModel> list = new List<Dcms_RoleModel>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Dcms_RoleModel item = new Dcms_RoleModel();
                if (ds.Tables[0].Rows[i]["Role_Id"].ToString() != "") item.Role_Id = int.Parse(ds.Tables[0].Rows[i]["Role_Id"].ToString());
                item.Role_Name = ds.Tables[0].Rows[i]["Role_Name"].ToString();
                if (ds.Tables[0].Rows[i]["Role_Order"].ToString() != "") item.Role_Order = int.Parse(ds.Tables[0].Rows[i]["Role_Order"].ToString());
                if (ds.Tables[0].Rows[i]["Role_level"].ToString() != "") item.Role_level = int.Parse(ds.Tables[0].Rows[i]["Role_level"].ToString());
                list.Add(item);
            }
            return list;
        }

        public void DeleteModelByID(int ID)
        {
            string sQLString = "delete from Dcms_Role where Role_Id=@Role_Id";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@Role_Id", OleDbType.Integer) };
            cmdParms[0].Value = ID;
            OleDbHelper.ExecuteSql(sQLString, cmdParms);
        }

        public string GetDataCount(string SqlStr)
        {
            return OleDbHelper.GetNoteCount(SqlStr).ToString();
        }

        public IList<Dcms_RoleModel> GetDataListByPage(string SqlStr, int CurPage, int PageSize)
        {
            return this.DataSetToList(OleDbHelper.CreateDataSet(SqlStr, (CurPage - 1) * PageSize, PageSize));
        }

        public int GetLevel(int RoleID)
        {
            string sQLString = "select Role_Level from  Dcms_Role where Role_Id=@Role_Id";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@Role_Id", OleDbType.Integer) };
            cmdParms[0].Value = RoleID;
            object single = OleDbHelper.GetSingle(sQLString, cmdParms);
            if (single != null) return int.Parse(single.ToString());
            return -1;
        }

        public Dcms_RoleModel GetModelByID(int ID)
        {
            string sQLString = "select * from Dcms_Role where Role_Id=@Role_Id";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@Role_Id", OleDbType.Integer) };
            cmdParms[0].Value = ID;
            Dcms_RoleModel model = new Dcms_RoleModel();
            DataSet ds = OleDbHelper.CreateDataSet(sQLString, cmdParms);
            IList<Dcms_RoleModel> list = this.DataSetToList(ds);
            return ((list.Count > 0) ? list[0] : null);
        }

        public IList<Dcms_RoleModel> GetModelList()
        {
            string sql = "select * from Dcms_Role";
            return this.DataSetToList(OleDbHelper.CreateDataSet(sql));
        }

        public Dcms_RoleModel Update(Dcms_RoleModel model)
        {
            string sQLString = "update Dcms_Role set Role_Name=@Role_Name,Role_Order=@Role_Order, Role_level=@Role_level where Role_Id=@Role_Id";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@Role_Name", OleDbType.VarChar), new OleDbParameter("@Role_Order", OleDbType.Integer), new OleDbParameter("@Role_level", OleDbType.Integer), new OleDbParameter("@Role_Id", OleDbType.Integer) };
            cmdParms[0].Value = model.Role_Name;
            cmdParms[1].Value = model.Role_Order;
            cmdParms[2].Value = model.Role_level;
            cmdParms[3].Value = model.Role_Id;
            OleDbHelper.ExecuteSql(sQLString, cmdParms);
            return model;
        }
    }
}

